package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.CollectActivity;
import flc.ast.activity.PptActivity;
import flc.ast.activity.PptDetailActivity;
import flc.ast.activity.SearchActivity;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.adapter.PptAdapter;
import flc.ast.adapter.TypeAdapter;
import flc.ast.bean.MyTypeBean;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private TypeAdapter typeAdapter;
    private PptAdapter pptAdapter;
    private int page = 1;
    private int refreshTime = 200;
    private List<StkTagResBean> listBanner = new ArrayList<>();
    private List<StkTagResBean> listType = new ArrayList<>();


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.ivCollect.setOnClickListener(this);

        mDataBinding.rvTypeList.setLayoutManager(new GridLayoutManager(mContext, 4));
        typeAdapter = new TypeAdapter();
        mDataBinding.rvTypeList.setAdapter(typeAdapter);
        typeAdapter.setOnItemClickListener(this);

        mDataBinding.rvRecommendList.setLayoutManager(new GridLayoutManager(mContext, 2));
        pptAdapter = new PptAdapter();
        mDataBinding.rvRecommendList.setAdapter(pptAdapter);
        pptAdapter.setOnItemClickListener(this);

        mDataBinding.refreshTabItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshTabItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshTabItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                mDataBinding.refreshTabItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                mDataBinding.refreshTabItem.finishRefresh(refreshTime);
            }
        });

        mDataBinding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String key = mDataBinding.etSearch.getText().toString().trim();
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if (key.isEmpty()) {
                        ToastUtils.showShort(getString(R.string.please_input_search));
                        return true;
                    }
                    SearchActivity.key = key;
                    startActivity(SearchActivity.class);
                }
                return false;
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜索
                String movieKey = mDataBinding.etSearch.getText().toString().trim();
                if (movieKey.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_search));
                    return;
                }
                SearchActivity.key = movieKey;
                startActivity(SearchActivity.class);
                break;
            case R.id.ivCollect://我的收藏
                startActivity(CollectActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == typeAdapter) {
            PptActivity.hashId = listType.get(position).getHashid();
            PptActivity.name = listType.get(position).getName();
            startActivity(PptActivity.class);
        } else if (adapter == pptAdapter) {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            PptDetailActivity.bean = bean;
            startActivity(PptDetailActivity.class);
        }
    }

    @Override
    protected void initData() {
        List<Integer> list = new ArrayList<>();
        list.add(R.drawable.kp_yxmb1);
        list.add(R.drawable.kp_yxmb2);
        list.add(R.drawable.kp_yxmb3);
        list.add(R.drawable.kp_yxmb4);
//        list.add(R.drawable.kp_yxmb5);
        MyBannerAdapter bannerAdapter = new MyBannerAdapter(list, getContext());
        mDataBinding.banner.setAdapter(bannerAdapter, true);//false不无限循环
        mDataBinding.banner.setBannerGalleryEffect(0, 40, 20, 1);
        mDataBinding.banner.isAutoLoop(true);//false不自动轮播
        mDataBinding.banner.setIndicator(new RectangleIndicator(mContext));
        //点击监听
        mDataBinding.banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                switch (position) {
                    case 0:
                        PptActivity.hashId = listBanner.get(2).getHashid();
                        PptActivity.name = getString(R.string.banner_name1);
                        break;
                    case 1:
                        PptActivity.hashId = listBanner.get(0).getHashid();
                        PptActivity.name = getString(R.string.banner_name2);
                        break;
                    case 2:
                        PptActivity.hashId = listBanner.get(1).getHashid();
                        PptActivity.name = getString(R.string.banner_name3);
                        break;
                    case 3:
                        PptActivity.hashId = listBanner.get(3).getHashid();
                        PptActivity.name = getString(R.string.banner_name4);
                        break;
                }
                startActivity(PptActivity.class);
            }
        });
        List<MyTypeBean> listType = new ArrayList<>();
        listType.add(new MyTypeBean(R.drawable.icon_rm, getString(R.string.ppt_type1)));
        listType.add(new MyTypeBean(R.drawable.icon_jy, getString(R.string.ppt_type2)));
        listType.add(new MyTypeBean(R.drawable.icon_qx, getString(R.string.ppt_type3)));
        listType.add(new MyTypeBean(R.drawable.icon_ss1, getString(R.string.ppt_type4)));
        typeAdapter.setList(listType);
        getData();
        getBannerData();
        getTypeData();
    }


    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.RECOMMEND_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            pptAdapter.setList(result);
                        } else {
                            pptAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void getBannerData() {
        StkResApi.getChildTagList(this, AppConfig.BANNER_DATA, StkResApi.createParamMap(0, 10), new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listBanner = result;
                    }
                }
            }
        });
    }

    private void getTypeData() {
        StkResApi.getChildTagList(this, AppConfig.HOME_DATA, StkResApi.createParamMap(0, 10), new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listType = result;
                    }
                }
            }
        });
    }

}